package org.otherworld.frame.filebrowser;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTree;

import org.otherworld.frame.filebrowser.action.FileBrowserCopy;
import org.otherworld.frame.filebrowser.action.FileBrowserDelete;
import org.otherworld.frame.filebrowser.action.FileBrowserEdit;
import org.otherworld.frame.filebrowser.action.FileBrowserNew;
import org.otherworld.frame.filebrowser.action.FileBrowserView;
import org.otherworld.frame.textwriter.TextWriter;

public class TreeMouseAdapter extends MouseAdapter {
	private JPopupMenu jpop;
	private JFrame frame;
	private JPanel panel;
	private JTree tree;

	public TreeMouseAdapter(JFrame frame, JPanel panel) {
		this.frame = frame;
		this.panel = panel;
		

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger()) {
			tree = (JTree)e.getSource();
			this.makePopupMenu(frame,tree);
			jpop.show(e.getComponent(), e.getX(), e.getY());	
			
			
		}
	}


	private void makePopupMenu(JFrame ancestor,JTree tree) {
		jpop = new JPopupMenu("File");
		JMenuItem copy = new JMenuItem("Copy");
		copy.setName("copy_command");
		copy.addActionListener(new FileBrowserCopy(panel,tree));
		JMenuItem newFile = new JMenuItem("New");
		newFile.addActionListener(new FileBrowserNew(panel,tree));
		JMenuItem delete = new JMenuItem("Delete");
		delete.addActionListener(new FileBrowserDelete(panel, tree));
		JMenuItem edit = new JMenuItem("Edit");
		edit.addActionListener(new FileBrowserEdit(panel, tree));
		JMenuItem view = new JMenuItem("View");
		view.addActionListener(new FileBrowserView(panel, tree));
		JMenuItem open = new JMenuItem("Open");
		jpop.add(newFile);
		jpop.add(copy);
		jpop.add(view);
		jpop.add(edit);
		
		jpop.add(delete);
		ancestor.add(jpop);
	}
}
